import 'package:dio/dio.dart';
import 'package:jyofservice/jicha/ToDoJiCha.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/addmenu/AddMenuPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jyofservice/delay/DelayPage.dart';
import 'package:jyofservice/generated/json/base/json_convert_content.dart';
import 'package:jyofservice/gongdandiaodu/GongDanDiaoDu.dart';
import 'package:jyofservice/init/FunctionNotReadPage.dart';
import 'package:jyofservice/jicha/JiChaInfo.dart';
import 'package:jyofservice/kaoqin/SignList.dart';
import 'package:jyofservice/tonghuajilu/CallRecord.dart';
import 'package:jyofservice/tongjichaxun/TongJiChaXun.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/util/SlideRoute.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:jyofservice/zuoxijianguan/SeatCharge.dart';
import 'package:jyofservice/bean/function_data_entity.dart';
import 'package:jyofservice/checkmenu/CheckPage.dart';

class FunctionMenu extends StatefulWidget {
  FunctionMenu({Key? key}) : super(key: key);
  @override
  FunctionMenuState createState() => FunctionMenuState();
}

class FunctionMenuState extends State<FunctionMenu> {

  int delayCount = 0;
  int? diaoCount = 0;
  int? myMenuCount = 0;
  int? cheakMenuCount = 0;
  int? zhuanjiaoMenuCount = 0;
  List<TextButton> menuList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getFunctionMenu();
  }

  @override
  Widget build(BuildContext context) {
    
  return Column(
    children: [
      Container(
        width: ScreenUtil().screenWidth,
        color: Colors.white,
        padding: EdgeInsets.fromLTRB(10, 5, 0, 5),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              width: ScreenUtil().setWidth(5),
              color: Colors.blue,
              height: ScreenUtil().setHeight(30),
            ),
            SizedBox(
              width: ScreenUtil().setWidth(15),
            ),
            Text(
              "客服工单管理", style: TextStyle(color: Colors.black87,
                fontSize: ScreenUtil().setSp(30),
                fontWeight: FontWeight.bold),
            )
          ],
        ),
      ),
      Container(
        color: Colors.black26,
        width: ScreenUtil().screenWidth,
        height: ScreenUtil().setHeight(1),
      ),
      GridView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            //横轴元素个数
            crossAxisCount: 3,
          ), itemCount: menuList.length,
          itemBuilder: (BuildContext context,int index){
            return menuList[index];
          })
    ],
  );

  }

  TextButton functionMenuWidget(String asset, String label,int count) {
    return TextButton(
        onPressed: () {

          switch (label) {
            case "工单查询":
              Navigator.push(context, SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMyAndChildrenAllWorkForm",title:"工单查询")));
              break;
            case "退回审批":
              Navigator.push(context, SlideRoute().createRoute(DelayPage())).then((value){

                getFunctionMenu();

              });
              break;
            case "统计查询":
              Navigator.push(context, SlideRoute().createRoute(TongJiChaXunPage()));
              break;
            case "坐席监管":
              Navigator.push(context, SlideRoute().createRoute(SeatChargePage()));
              break;
            case "通话记录" :
              Navigator.of(context)
                  .push(SlideRoute().createRoute(CallRecordPage()));
              break;
            case "工单调度":
              Navigator.of(context)
                  .push(SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMyAndChildrenWorkForm",title:"工单调度"))).then((date){

                getFunctionMenu();

              });
              break;
            case "我的工单":
              Navigator.of(context)
                  .push(SlideRoute().createRoute(GongDanDiaoDuPage(
                  view: "GetMySelfWaitDoWorkConfirm", title: "我的工单"))).then((date) {

                getFunctionMenu();

              });
              break;
            case "已处理工单":
              Navigator.push(context, SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMyAllFinishWork",title:"已处理工单")));
              break;
            // case "考勤签到":
            //   Navigator.push(context, SlideRoute().createRoute(SignList()));
            //   break;
            // case "稽查信息":
            // Navigator.push(context, SlideRoute().createRoute(ToDoJiCha()));
            // break;
            case "工单补录":
              Navigator.push(context, SlideRoute().createRoute(AddMenuPage()));
              break;
            case "轨迹定位":
              break;
            case "确认工单":
              Navigator.of(context)
                  .push(SlideRoute().createRoute(CheckPge(functionid: 0,))).then((date) {
                getFunctionMenu();
              });
              break;
            case "工单转交":
              Navigator.of(context)
                  .push(SlideRoute().createRoute(CheckPge(functionid: 1,))).then((date) {
                getFunctionMenu();
              });
              break;

          }
        },
        child: Container(
          height: ScreenUtil().setHeight(230),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Stack(
                alignment: Alignment(1.6, -1.0),
                children: <Widget>[
                  Image.asset(
                    asset,
                    width: ScreenUtil().setWidth(120),
                    height: ScreenUtil().setHeight(120),
                  ),

                  Offstage(
                    offstage: count>0?false:true,
                    child:  Container(
                      alignment: Alignment.center,
                      width: ScreenUtil().setWidth(50),
                      height:  ScreenUtil().setWidth(50),
                      decoration: BoxDecoration(
                        color: Colors.red,
                        borderRadius: BorderRadius.circular(ScreenUtil().setWidth(30.0)),
                      ),
                      child: Center(child:Text(count>999?"999+":count.toString(),style: TextStyle(
                          fontSize: ScreenUtil().setSp(22),
                          color: Colors.white,fontWeight: FontWeight.w500),) ,),
                    ),
                  )

                ],
              ),
              SizedBox(
                height: ScreenUtil().setHeight(5),
              ),
              Text(
                label,
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(30),
                  color: Colors.black,
                ),
              )
            ],
          ),
        )
    );
  }

  Future getFunctionMenu() async{
    menuList.clear();
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData = await AesUtil.encodeBase64();
    var data = {
      "key": key,
      "ClientType": "phone",
      "ClientKey": aesData
    };
    print(data.toString());

    Response response = await Dio().post(
        WebConfig().baseUrl+ "DB/getfunctionbykey",
        data: data);
    print("权限工单"+response.data.toString());

    if (response.data["Code"] == "200") {
      FunctionDataEntity? functionDataEntity = FunctionDataEntity().fromJson(response.data);
      // 青铜峡项目也有补录功能, 2021-11-23修改
      // 只有职务是调度的有此权限
      if(sharedPreferences.getInt("postid")==3) {
        menuList.add(functionMenuWidget("images/bulu.png","工单补录",0),);
      }
      if(functionDataEntity != null && functionDataEntity.data != null){
        for(int i =0;i<functionDataEntity.data!.length;i++) {
          switch (functionDataEntity.data![i]!.url) {
            case "Work/AllWorkForListener":
              menuList.add(functionMenuWidget("images/gongdanchaxun.png","工单查询",0),);
              break;
            case "Work/HuanBanShenHe":
              await FunctionNotRead().getDelyData().then((value) => delayCount = value);
              menuList.add(functionMenuWidget("images/delay.png","退回审批",delayCount));
              break;
            case "":
              if(functionDataEntity.data![i]!.isParent==1&&functionDataEntity.data![i]!.functionName=="统计查询")
                menuList.add(functionMenuWidget("images/tognjichaxun.png","统计查询",0),);
              break;
            case "Phone/SeatControl":
              menuList.add(functionMenuWidget("images/zuoxizhongtaijianguan.png","坐席监管",0),);
              break;
            case "Phone/Phone_history":
              menuList.add(functionMenuWidget("images/tonghuajilu.png","通话记录",0),);
              break;
            case "Work/WaitDo":
              await FunctionNotRead().getDiaoDuListData().then((value) => diaoCount = value);
              menuList.add(functionMenuWidget("images/gongdandiaodu.png","工单调度",diaoCount!),);
              break;
            case "Work/MyWork":
              await FunctionNotRead().getWoDeListData().then((value) => myMenuCount = value);
              menuList.add(functionMenuWidget("images/wodegongdan.png","我的工单",myMenuCount!),);
              break;
            case "Work/MyFinishWork":
              menuList.add(functionMenuWidget("images/yichuligongdan.png","已处理工单",0));
              break;
          // case "ShouGuang/WorkTimeHistory":
          //   menuList.add(functionMenuWidget("images/kaoqinqiandao.png","考勤签到",0),);
          //   break;
          // case "ShouGuang/HouseInfoJC":
          //   menuList.add(functionMenuWidget("images/jichaxingxi.png","稽查信息",0),);
          //   break;
          // case "Work/GuiJi":
          //   menuList.add(functionMenuWidget("images/position.png","轨迹定位",0),);
          //   break;
            case "Work/KouXianTiZhengZhi":
              await FunctionNotRead().getCheckData().then((value) => cheakMenuCount = value);
              menuList.add(functionMenuWidget("images/check.png","确认工单",cheakMenuCount!),);
              break;
            case 'Work/AllWorkZhuanJiao':
              await FunctionNotRead().getCheckData().then((value) => zhuanjiaoMenuCount = value);
              menuList.add(functionMenuWidget("images/gongdandiaodu.png","工单转交",zhuanjiaoMenuCount!),);
              break;

          }
        }
      }
      if(mounted) {
        setState(() {

        });
      }
    }
  }

}
